import by.zmy.aop.MethodInvocation;
import by.zmy.aop.Pointcut;
import by.zmy.aop.PointcutAspect;

import java.lang.reflect.Method;
import java.util.Arrays;

// 2. 定义切面
public class LogAspect extends PointcutAspect {
    public LogAspect() {
        super(Pointcut.forMethodName("getUser"));
    }

    @Override
    protected void doBefore(Object target, Method method, Object[] args) {
        System.out.println("[Log] 准备执行方法: " + method.getName() + ", 参数: " + Arrays.toString(args));
    }

    @Override
    protected void doAfter(Object target, Method method, Object[] args, Object result) {
        System.out.println("[Log] 方法执行完成: " + method.getName() + ", 结果: " + result);
    }

    @Override
    protected Object doAround(Object target, Method method, Object[] args, MethodInvocation invocation) throws Throwable {
        long start = System.currentTimeMillis();
        try {
            return invocation.proceed();
        } finally {
            long end = System.currentTimeMillis();
            System.out.println("[Log] 方法执行耗时: " + (end - start) + "ms");
        }
    }

    @Override
    public void afterThrowing(Object target, Method method, Object[] args, Throwable throwable) {
        System.out.println("afterThrowing   "+throwable.getMessage());
        super.afterThrowing(target, method, args, throwable);
    }
}